package com.karry.yl.web.elder;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.karry.commons.base.SuperController;
import com.karry.commons.vo.JsonResponse;
import com.karry.commons.vo.LayTableResponse;
import com.karry.yl.domain.elder.CheckInRecords;
import com.karry.yl.domain.elder.CheckOutRecords;
import com.karry.yl.domain.elder.Elderly;
import com.karry.yl.service.elder.CheckInRecordsService;
import com.karry.yl.service.elder.CheckOutRecordsService;
import com.karry.yl.service.elder.ElderlyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 入住记录Controller
 */

@Controller
@RequestMapping("/checkoutrecords")
public class CheckOutRecordsController extends SuperController {
    @Autowired
    private ElderlyService elderlyService;
    @Autowired
    private CheckInRecordsService checkInRecordsService;
    @Autowired
    private CheckOutRecordsService checkOutRecordsService;

    @RequestMapping
    public String listUI(){
        return "elder/checkoutrecords/list";
    }

    /**
     * 离住
     *
     * @param elderlyId
     * @param map
     * @return
     */
    @RequestMapping("/checkOut")
    public String checkOut(Long elderlyId, ModelMap map) {
        //根据老人id查询出老人信息
        Elderly elderly = elderlyService.selectById(elderlyId);
        map.put("elderly", elderly);
        CheckInRecords checkInRecords = checkInRecordsService.selectOne(new EntityWrapper<CheckInRecords>().eq("elderlyId", elderly.getId()).eq("orgId", currentUser.getOrgId()).eq("deleted", false));
        map.put("checkInRecords", checkInRecords);
        return "elder/elderly/checkOut";
    }



    @RequestMapping("/list")
    @ResponseBody
    public LayTableResponse listUI(int page, int limit){
        Page<CheckOutRecords> checkOutRecordsPage = checkOutRecordsService.selectPage(new Page<CheckOutRecords>(page, limit),new EntityWrapper<CheckOutRecords>().eq("orgId", currentUser.getOrgId()).eq("deleted",false).orderBy("createdTime",false));
        return new LayTableResponse(checkOutRecordsPage.getRecords(),checkOutRecordsPage.getTotal());
    }

    @RequestMapping("/save")
    @ResponseBody
    public JsonResponse save(CheckOutRecords checkOutRecords){
        try {
            checkOutRecords.setOrgId(currentUser.getOrgId());
            checkOutRecords.setOrgName(currOrganization.getName());
            CheckInRecords checkInRecords = checkInRecordsService.selectOne(new EntityWrapper<CheckInRecords>().eq("elderlyId", checkOutRecords.getElderlyId()).eq("orgId", currentUser.getOrgId()).eq("deleted",false));
            checkOutRecords.setCheckInRecordsId(checkInRecords.getId());
            checkOutRecordsService.save(checkOutRecords,checkInRecords);
            return new JsonResponse(200,"退院成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResponse(0,"退院失败，请联系管理员");
        }
    }

    /**
     * 删除
     * @param
     * @return
     */
    @RequestMapping(path = "/delete")
    public String delete(@RequestParam("id") List<Long> ids){
        checkOutRecordsService.deleteBatchIds(ids);
        return "redirect:/checkoutrecords";
    }
}
